package com.blue.controller.author;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.annotation.NotNull;
import com.blue.common.annotation.ReplaceParam;
import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.ConvertUtils;
import com.blue.common.utils.QueryWrapperUtils;
import com.blue.dto.author.UserDTO;
import com.blue.po.author.UserPO;
import com.blue.service.author.IUserService;
import com.blue.service.author.IUserService;
import com.blue.vo.author.UserVO;
import com.github.yulichang.query.MPJQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户表
 *
 * @author cnj
 * @email 15696471158@163.com
 * @date 2024-03-17
 */
@RestController
@RequestMapping("/author/user")
@Tag(name = "用户表")
public class UserController {
    @Autowired
    private IUserService UserPOService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    @ReplaceParam("username,phone")
    public ResponseResult<?> list(UserDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        UserPO UserPO = ConvertUtils.convert(UserPO.class, requestDTO);
        MPJQueryWrapper<UserPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(UserPO);
        Page<UserPO> page = new Page<>(pageNum, pageSize);
        Page<UserPO> pageResult = UserPOService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    @NotNull(value = "userNo", message = "账号不能为空")
    @NotNull(value = "password", message = "密码不能为空")
    public ResponseResult<?> add(@RequestBody UserDTO requestDTO) throws Exception {
        UserPOService.add(requestDTO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    @NotNull(value = "userId", message = "userId不能为空")
    public ResponseResult<?> edit(@RequestBody UserDTO requestDTO) {
        UserPOService.edit(requestDTO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    @NotNull(value = "ids", message = "ids不能为空")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        UserPOService.delete(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    @NotNull(value = "userId", message = "userId不能为空")
    public ResponseResult<?> queryById(@RequestBody UserDTO requestDTO) {
        UserVO UserVo = UserPOService.queryById(requestDTO.getUserId());
        return ResponseResult.okResult(UserVo);
    }

    @PostMapping("/offline")
    @Operation(summary = "离线")
    @NotNull(value = "userId", message = "用户id不能为空")
    public ResponseResult<?> delete(@RequestBody UserDTO requestDTO) {
        UserPOService.offline(requestDTO.getUserId());
        return ResponseResult.okResult();
    }
}
